const Comment = require('../models/commentModel');

class CommentController {
    static async createComment(req, res) {
        try {
            const { news_id, content } = req.body;
            const user_id = req.user.userId || req.user.user_id; 

            console.log('Creating comment with:', { news_id, user_id, content });
            console.log('User from token:', req.user);

            const commentId = await Comment.create({
                news_id,
                user_id,
                content
            });

            res.status(201).json({
                success: true,
                message: 'Comment created successfully',
                commentId
            });
        } catch (error) {
            res.status(500).json({
                success: false,
                message: 'Error creating comment',
                error: error.message
            });
        }
    }

    static async getNewComments(req, res) {
        try {
            const newsId = req.params.newsId;
            const page = parseInt(req.query.page) || 1;
            const limit = parseInt(req.query.limit) || 10;

            const comments = await Comment.getByNewsId(newsId, page, limit);
            res.json({
                success: true,
                data: comments
            });
        } catch (error) {
            res.status(500).json({
                success: false,
                message: 'Error fetching comments',
                error: error.message
            });
        }
    }

    static async deleteComment(req, res) {
        try {
            const { commentId, newsId } = req.params; 
            const userId = req.user.userId || req.user.user_id; 
            const isAdmin = req.user.role === 'admin'; // Check if user is admin

            console.log('Deleting comment:', { commentId, newsId, userId, isAdmin });
            console.log('User from token:', req.user);

            // Check if comment exists
            const comment = await Comment.findById(commentId);
            console.log('Found comment:', comment);

            if (!comment) {
                return res.status(404).json({
                    success: false,
                    message: 'Comment not found'
                });
            }

            // Allow deletion if user is admin or the comment owner
            if (!isAdmin && comment.user_id !== userId) {
                return res.status(403).json({
                    success: false,
                    message: 'Not authorized to delete this comment'
                });
            }

            const deleted = await Comment.delete(commentId, newsId);
            if (deleted) {
                res.json({
                    success: true,
                    message: 'Comment deleted successfully'
                });
            } else {
                res.status(400).json({
                    success: false,
                    message: 'Failed to delete comment'
                });
            }
        } catch (error) {
            console.error('Error in deleteComment:', error);
            res.status(500).json({
                success: false,
                message: 'Error deleting comment',
                error: error.message
            });
        }
    }
}

module.exports = CommentController; 